package com.ls.lsbase.frag;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.ls.lsbase.R;
import com.ls.lsbase.utils.ShareUtils;
import com.ls.lsbase.utils.ToastUtil;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;

/**
 * 分享基类
 * Created by ejl_001 on 2016/12/22.
 */
@SuppressLint("ValidFragment")
public class ShareFrag extends AppCompatDialogFragment {
    private RelativeLayout wechat;
    private RelativeLayout wxcircle;
    private RelativeLayout qq;
    private RelativeLayout qzone;

    String TITLE;
    String DESC;
    String TURN_URL;
    String IMG_URL;

    public ShareFrag(String title, String desc, String turnUrl, String imgUrl) {
        this.TITLE = title;
        this.DESC = desc;
        this.TURN_URL = turnUrl;
        this.IMG_URL = imgUrl;
        if (TextUtils.isEmpty(TITLE) || TextUtils.isEmpty(DESC) || TextUtils.isEmpty(TURN_URL) || TextUtils.isEmpty(IMG_URL)) {
            getDialog().dismiss();
        }
    }

    IUiListener listener = new IUiListener() {
        @Override
        public void onComplete(Object o) {
        }

        @Override
        public void onError(UiError uiError) {
            ToastUtil.showShort(getActivity(), "出错了," + uiError.errorMessage);
        }

        @Override
        public void onCancel() {
            ToastUtil.showShort(getActivity(), "已取消");
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_share_layout, container, false);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        Dialog dialog = new Dialog(getActivity(), R.style.Share_BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(R.layout.fragment_share_layout);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);
        return dialog;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        wechat = (RelativeLayout) view.findViewById(R.id.wechat);
        wxcircle = (RelativeLayout) view.findViewById(R.id.wxcircle);
        qq = (RelativeLayout) view.findViewById(R.id.qq);
        qzone = (RelativeLayout) view.findViewById(R.id.qzone);
        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareUtils.shareToQQ(getActivity(), TITLE, DESC, TURN_URL, IMG_URL, listener);
                getDialog().dismiss();
            }
        });
        qzone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareUtils.shareToQzone(getActivity(), TITLE, DESC, TURN_URL, IMG_URL, listener);
                getDialog().dismiss();
            }
        });
        wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareUtils.shareToWx(getActivity(), TITLE, DESC, TURN_URL, IMG_URL, false);
                getDialog().dismiss();
            }
        });
        wxcircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareUtils.shareToWx(getActivity(), TITLE, DESC, TURN_URL, IMG_URL, true);
                getDialog().dismiss();
            }
        });
    }
}
